package ar.edu.itba.it.pdc.httpParser;

import ar.edu.itba.it.pdc.httpParser.exceptions.HttpBadRequestException;
import ar.edu.itba.it.pdc.httpParser.models.HttpBody;
import ar.edu.itba.it.pdc.httpParser.models.HttpRequest;

public class HttpRequestParser extends HttpParser {
	
	public HttpRequestParser(Lexer lexer) {
		this.lexer = lexer;
		it = new RequestLineParser();
		model = new HttpRequest();
	}

	public int setBodyParser() throws HttpBadRequestException {
		if (model.getHeaderValue("Content-Length") != null) {
			state = State.BODY;
			int bytesToRead = Integer.parseInt(model.getHeaderValue("Content-Length"));
			bp = new LengthBodyParser(bytesToRead);

			if (!connectionClose && bytesToRead <= 0) {
				model.setBody((HttpBody)bp.getModel());

				state = State.END;
				return 0;
			}
			return bytesToRead;
		} else {
			state = State.END;
			bp = new NoBoadyParser();
			model.setBody((HttpBody)bp.getModel());
			return 0;
		}
	}

}
